/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spring.app.genpostagelabel;

import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormatter;

/**
 *
 * @author ashwani
 */
public class ExcelManager {
    
    private HSSFWorkbook workBook;

    public HSSFWorkbook getWorkBook() {
        return workBook;
    }

    public void setWorkBook(HSSFWorkbook workBook) {
        this.workBook = workBook;
    }

    public int totalRowsToProcess(HSSFSheet sheet)
    {
        String data;
        if(sheet == null)
        {
            System.out.println("Error!! Null sheet reference");
            return 0;
        }
        Iterator rowIter = sheet.rowIterator();
        HSSFRow r = (HSSFRow)rowIter.next();
        short lastCellNum = r.getLastCellNum();
        int[] dataCount = new int[lastCellNum];
        int col = 0;
        int rowCount = 0;
        rowIter = sheet.rowIterator();
        while(rowIter.hasNext()) {
            rowCount++;
            HSSFRow nextRow = (HSSFRow)rowIter.next();
            System.out.print("row no= <"+rowCount+">, contents = <");
            //System.out.println("row = "+rowIter.toString());
            //Iterator cellIter = ((HSSFRow)rowIter.next()).cellIterator();
            Iterator cellIter = nextRow.cellIterator();
            while(cellIter.hasNext()) {
                HSSFCell cell = (HSSFCell)cellIter.next();
                col = cell.getColumnIndex();
                dataCount[col] += 1;
                HSSFDataFormatter df = new HSSFDataFormatter();
                data = df.formatCellValue(cell);
                System.out.print(data+",");
            }
            System.out.println(">");
        }
        //is.close();
//        for(int x = 0; x < dataCount.length; x++) {
//            //System.out.println("col " + x + ": " + dataCount[x]);
//        }
        return rowCount;
    }
    
    
}
